package com.example.yls.cnbetaapp.activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.yls.cnbetaapp.R;
import com.example.yls.cnbetaapp.component.OnTvGlogbalLayoutListener;
import com.example.yls.cnbetaapp.entity.ArticleEntity;
import com.example.yls.cnbetaapp.httpService.ArticleService;

public class ContentActivity extends AppCompatActivity {
    private ArticleHander articleHander;
    final ArticleService articleService = new ArticleService();
    private TextView mText;
    private class ArticleHander extends Handler{
        @Override
        public void handleMessage(Message msg){
            ArticleEntity articleEntity = (ArticleEntity) msg.obj;
            TextView text_title = (TextView) findViewById(R.id.article_title);
            ImageView summary_img = (ImageView) findViewById(R.id.article_summary_img);
            mText =(TextView) findViewById(R.id.article_summary);
            text_title.setText(articleEntity.getArticle_title());
            mText.setText("\u3000\u3000"+articleEntity.getArticle_summary());
            mText.getViewTreeObserver().addOnGlobalLayoutListener(new OnTvGlogbalLayoutListener(mText));
            articleService.setSummaryImg(summary_img,articleEntity);
            LinearLayout article_contentLayout = findViewById(R.id.article_content);
            articleService.setArticleContent(ContentActivity.this,article_contentLayout,articleEntity);
//            ((TextView)findViewById(R.id.aa)).setText("1323232");
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.article);
        Intent intent = getIntent();
        final int mid = Integer.parseInt(intent.getStringExtra("mid"));
        articleHander = new ArticleHander();
         Thread thread = new Thread(new Runnable() {
             @Override
             public void run() {
                 articleService.sendArticleToMessageQue(articleHander,mid);
             }
         });
        thread.start();
//        webView = (WebView) findViewById(R.id.webview);
//        webView.setWebViewClient(new WebViewClient() {
//            //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                return true;
//            }
//        });
//        webView.getSettings().setJavaScriptEnabled(true);  //设置WebView属性,运行执行js脚本
//        Intent intent = getIntent();
//        webView.loadUrl(intent.getStringExtra("titleUrl"));//调用loadUrl方法为WebView加入链接
//        webView.setInitialScale(70);
//        WebSettings settings = webView.getSettings();
//        settings.setUseWideViewPort(true);//设定支持viewport
//        settings.setLoadWithOverviewMode(true);   //自适应屏幕
//        settings.setBuiltInZoomControls(true);
//        settings.setDisplayZoomControls(false);
//        settings.setSupportZoom(true);//设定支持缩放
    }
//    @Override
//    public void onBackPressed() {
//        if (webView.canGoBack()) {
//            webView.goBack();
//        }else{
//            this.finish();
//        }
//    }
//public class OnTvGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
//    @Override
//    public void onGlobalLayout() {
//        mText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//        final String newText = autoSplitText(mText);
//        if (!TextUtils.isEmpty(newText)) {
//            mText.setText(newText);
//        }
//    }
//}
//
//
//    public String autoSplitText(final TextView tv) {
//        final String rawText = tv.getText().toString(); //原始文本
//        final Paint tvPaint = tv.getPaint(); //paint，包含字体等信息
//        final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度
//
//        //将原始文本按行拆分
//        String rawTextLine = rawText.replaceAll("\r\n","");
//        StringBuilder sbNewText = new StringBuilder();
//                float lineWidth = 0;
//                for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {
//                    char ch = rawTextLine.charAt(cnt);
//                    lineWidth += tvPaint.measureText(String.valueOf(ch));
//                    if (lineWidth <= tvWidth) {
//                        sbNewText.append(ch);
//                    } else {
//                        sbNewText.append("\n");
//                        lineWidth = 0;
//                        --cnt;
//                    }
//                 }
//        return sbNewText.toString();
//    }

}
